package com.cyz.user.userService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.Comment;
import com.cyz.common.dto.CommentDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.CommentMapper;
import com.cyz.user.userService.CommentService;
import com.cyz.user.userService.User_UserService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.user.vo.CommentVo;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 34928
* @description 针对表【comment(评论表)】的数据库操作Service实现
* @createDate 2023-02-22 10:23:10
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService {

    @Autowired
    private User_UserService userUserService;

    @Override
    public R queryCardComments(Integer id) {
        //创建筛选器
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询该课程id下面的评论
        lambdaQueryWrapper.eq(Comment::getCardId,id);
        //查询根评论
        lambdaQueryWrapper.eq(Comment::getRootId, MyEnums.COMMENT_ROOD_ID);
        //查询数据
        List<Comment> list = list(lambdaQueryWrapper);
        //vo处理
        List<CommentVo> commentVos = BeanCopyUtil.copyBeanList(list, CommentVo.class);
        //匹配子评论
        toCommentVoList(commentVos,MyEnums.COMMENT_TYPE_CARD);
        //无需错误判断，因为评论数据可以为空
        return R.success(commentVos);
    }

    @Override
    public R queryHelpComments() {
        //创建筛选器
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询课程评论
        lambdaQueryWrapper.eq(Comment::getType, MyEnums.COMMENT_TYPE_HELP);
        //查询根评论
        lambdaQueryWrapper.eq(Comment::getRootId, MyEnums.COMMENT_ROOD_ID);
        //查询数据
        List<Comment> list = list(lambdaQueryWrapper);
        //vo处理
        List<CommentVo> commentVos = BeanCopyUtil.copyBeanList(list, CommentVo.class);
        //匹配子评论
        toCommentVoList(commentVos,MyEnums.COMMENT_TYPE_HELP);
        //无需错误判断，因为评论数据可以为空
        return R.success(commentVos);
    }

    @Override
    public R addComment(CommentDto commentDto) {
        //dto转化
        Comment comment = BeanCopyUtil.copyBean(commentDto, Comment.class);
        //添加数据
        boolean flag = save(comment);
        //错误处理
        if(flag){
            //判断返回数据类型
            if(StringUtils.isBlank(ObjectUtils.toString(comment.getCardId()))){
                return queryHelpComments();
            }
            return queryCardComments(Math.toIntExact(comment.getCardId()));
        }
        return R.error(MyEnums.ADD_FAIL);
    }

    private List<CommentVo> toCommentVoList(List<CommentVo> commentVos,String type) {
        //遍历赋值
        commentVos
                .forEach(item -> {
                    item.setUsername(userUserService.getById(item.getCreateBy()).getNickName());
                    if (item.getToCommentUserId() != -1) {
                        item.setToCommentUserName(userUserService.getById(item.getToCommentUserId()).getNickName());
                    }
                    item.setAvatar(userUserService.getAvatarById(item.getCreateBy()));
                    item.setChildren(getChildren(item.getId(),type,item.getCardId()));
                });
        return commentVos;
    }

    private List<CommentVo> getChildren(Integer id,String type,Integer CardId) {
        //创建筛选器
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询评论类型
        lambdaQueryWrapper.eq(Comment::getType, type);
        //类型判断
        if(type.equals(MyEnums.COMMENT_TYPE_CARD)){
            //查询该课程id下面的评论
            lambdaQueryWrapper.eq(Comment::getCardId, CardId);
        }
        //查询根评论为id
        lambdaQueryWrapper.eq(Comment::getRootId, id);
        //根据创建时间排序
        lambdaQueryWrapper.orderByAsc(Comment::getCreateTime);
        //查询数据
        List<Comment> list = list(lambdaQueryWrapper);
        //vo处理
        List<CommentVo> commentVos = BeanCopyUtil.copyBeanList(list, CommentVo.class);
        //递归查询子评论
        return toCommentVoList(commentVos,type);
    }

}




